package com.macaku.utils;

import com.macaku.config.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-27
 * Time: 0:00
 */
@Slf4j
public class MediaUtils {

    // 获取UUID
    public static String getUUID_32() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     *
     */
    public static String getImageUniquePath(String originName) {
        String path = AppConfig.MAP_ROOT;
        // 获取唯一id
        String id = getUUID_32();
        //获取文件后缀
        String suffix = originName.substring(originName.lastIndexOf("."));

        //拼接
        path += id + suffix;
        return path;
    }

    public static File loadFile(MultipartFile file, String path) {
        try {
            File loadFile = new File(path);
            file.transferTo(loadFile);
            return loadFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
